//
//  AlbumListCell.m
//  SuperRadio
//
//  Created by supercchome on 14/12/17.
//  Copyright (c) 2014年 三个臭皮匠. All rights reserved.
//

#import "AlbumListCell.h"
#import "AlbumOfList.h"
#import "UIImageView+WebCache.h"


@interface AlbumListCell ()

//图片
@property(nonatomic, retain)UIImageView *AlbumImageView;
@property(nonatomic, retain)UIImageView *AlbumSmallImageView;//辑

@property(nonatomic, retain)UILabel *AlbumNameLable;//大标题
@property(nonatomic, retain)UIImageView *AlbumBigImageView;//小三角
@property(nonatomic, retain)UILabel *AlbumNumberLable;//人数
@property(nonatomic, retain)UILabel *AlbumDateLable;//日期

@end

@implementation AlbumListCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.AlbumImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_AlbumImageView];
        [_AlbumImageView release];
        
        self.AlbumSmallImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:_AlbumSmallImageView];
        [_AlbumSmallImageView release];
        
        self.AlbumNameLable = [[UILabel alloc]init];
        [self.contentView addSubview:_AlbumNameLable];
        _AlbumNameLable.numberOfLines = 2;
        [_AlbumNameLable release];
        
        self.AlbumBigImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:_AlbumBigImageView];
        [_AlbumBigImageView release];
        
        self.AlbumNumberLable = [[UILabel alloc]init];
        [self.contentView addSubview:_AlbumNumberLable];
        self.AlbumNumberLable.font = [UIFont systemFontOfSize:12];
        [_AlbumNumberLable release];
        
        self.AlbumDateLable = [[UILabel alloc]init];
        [self.contentView addSubview:_AlbumDateLable];
        self.AlbumDateLable.font = [UIFont systemFontOfSize:14];
        [_AlbumDateLable release];
       
    }
    return self;
}




//页面布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.contentView.frame.size.width/3;
    CGFloat height = self.contentView.frame.size.height/4;
    
    
    _AlbumImageView.frame = CGRectMake(10, 10, height * 4 - 20, height * 4 - 20);
    
    _AlbumSmallImageView.frame = CGRectMake(width, 10, height, height);
    
    _AlbumNameLable.frame = CGRectMake(width + height+5, 10, width * 2 - height, height * 2 - 10);
    
    _AlbumBigImageView.frame = CGRectMake(width+10, height * 2+8,height / 2 , height/2);
    //播放次数
    _AlbumDateLable.frame = CGRectMake(width+10+height/2 , height * 2, width*2 - height/2, height);
    //最后更新时间
    _AlbumNumberLable.frame = CGRectMake(width+10, height*3, width*2, height);
    [_AlbumNameLable sizeToFit];

}


- (void)setAlbumOfList:(AlbumOfList *)albumOfList
{
    if (_albumOfList != albumOfList) {
        [_albumOfList release];
        _albumOfList = [albumOfList retain];
    }
    
    // 专辑的图片
    [_AlbumImageView setImageWithURL:[NSURL URLWithString:self.albumOfList.albumCoverUrl290]];
    
    _AlbumNameLable.text = albumOfList.title;
    
    // 时间戳的转换
    // 从1970年1月1日到现在的时间再除以1000 (因为album.lastUptrackAt是微秒), 得到秒数
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:([albumOfList.lastUptrackAt integerValue] / 1000)];
    // 在NSDate 和 NSString 之间相互转换的格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置一个日期格式,让字符串能按照设定的格式转为相应的日期
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    // 将时间转换为字符串
    NSString *strDate = [formatter stringFromDate:date];
    _AlbumNumberLable.text = [NSString stringWithFormat:@"最近更新 %@", [strDate substringToIndex:10]];
    
    // 播放次数
    if ([albumOfList.playsCounts floatValue] > 10000) {
        
        CGFloat PlayCounts = [albumOfList.playsCounts floatValue] / 10000;
        self.AlbumDateLable.text = [NSString stringWithFormat:@"%0.2lf 万", PlayCounts];
    } else {
        self.AlbumDateLable.text = [NSString stringWithFormat:@"%@", albumOfList.playsCounts];
    }
//    _AlbumDateLable.text = [albumOfList.playsCounts description];
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"bofang" ofType:@"png"];
    _AlbumBigImageView.image = [UIImage imageWithContentsOfFile:path];
    
    NSString *pathJi = [[NSBundle mainBundle]pathForResource:@"ttpodicon" ofType:@"png"];
    _AlbumSmallImageView.image = [UIImage imageWithContentsOfFile:pathJi];

    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)dealloc
{
    [_AlbumImageView release];
    [_AlbumSmallImageView release];
    [_AlbumNameLable release];
    [_AlbumBigImageView release];
    [_AlbumNumberLable release];
    [_AlbumDateLable release];
    [super dealloc];
}






@end
